package com.zxl.live.screen.screen.ui.widget.panel;

import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.zxl.live.screen.screen.ui.widget.panel.heart.HeartSpirit;
import com.zxl.live.screen.screen.utils.ScreenUtils;

import java.util.Random;

public class HeartScreenPanel extends ScreenSpirit {
	
	private boolean isAddAlpha = false;
	
	private int count;
	
	private int alpha = 255;
	
	private int offsetX = 0;
	
	private int offsetY;
	
	private HeartSpirit left;

	private HeartSpirit right;


	public HeartScreenPanel(Bitmap bitmap, Random mRandom) {
		super(bitmap, mRandom);
		left = new HeartSpirit();
		right = new HeartSpirit();
	}
	
	@Override
	public void doDraw(Camera camera, Matrix matrix, Canvas canvas, Paint paint) {
		if (offsetX == 0) {
			offsetX = ScreenUtils.sWidthPixels / 2;
			offsetY = ScreenUtils.sHeightPixels / 2 - 55;
		}
		drawleft(canvas, paint);
		drawRight(canvas, paint);
	}
	
	@Override
	public void logic() {
		if (offsetX == 0) {
			offsetX = ScreenUtils.sWidthPixels / 2;
			offsetY = ScreenUtils.sHeightPixels / 2 - 55;
		}
		if (left.isFinish && right.isFinish) {
			count ++;
			if (count < 200) {		//闪烁
				if (count > 100) {
					if (isAddAlpha) {
						alpha += 10;
					} else {
						alpha -= 10;
					}
					if (alpha <= 0) {
						alpha = 0;
						isAddAlpha = true;
					} else if (alpha >= 255) {
						alpha = 255;
						if (count % 7 == 0) {
							isAddAlpha = false;
						}
					}
				}
			} else if (count == 100) {
				alpha = 255;
			} else {		//渐变消失
				alpha --;
				if (alpha <= 0) {
					count = 0;
					left.reset();
					right.reset();
					alpha = 255;
				}
			}
		} 
		logicleft();
		logicRight();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}
	
	private void drawleft(Canvas canvas, Paint paint) {
		int save = canvas.save();
		canvas.scale(0.5f, 0.5f, ScreenUtils.sWidthPixels / 2, ScreenUtils.sHeightPixels / 2);
		canvas.translate(-offsetX / 4, 0);
		left.draw(canvas, paint, alpha);
		canvas.restoreToCount(save);
	}
	
	private void drawRight(Canvas canvas, Paint paint) {
		if (left.isFinish) {
			int save = canvas.save();
			canvas.scale(0.5f, 0.5f, ScreenUtils.sWidthPixels / 2, ScreenUtils.sHeightPixels / 2);
			canvas.translate(offsetX / 4, 0);
			right.draw(canvas, paint, alpha);
			canvas.restoreToCount(save);
		}
	}

	private void logicleft() {
		left.logic(offsetX, offsetY);
	}
	
	private void logicRight() {
		if (left.isFinish) {
			right.logic(offsetX, offsetY);
		}
	}
}
